package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JasonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/travels")
public class TravelController {
    //注入travelService对象
    @Autowired
    private ITravelService travelService ;
    @Autowired
    private ITravelCommendService travelCommendService ;
    @Autowired
    private ICommentService commentService ;











    /*
    获取这篇游记的所有评论
    参数：
    资源：/travels/{travelId}/comments
    动作：GET
     */
    @GetMapping("/{travelId}/comments")
    public Object getComments(CommentQueryObject qo){
        qo.setOrderBy("c.creatTime desc");
        return commentService.queryForLits(qo);
    }




    /**
     * 获取全部的游记
     * 参数：currentPage,pageSize
     * 资源：/travels
     * 动作：GET
     */
    @GetMapping
    public Object getTravels( TravelQueryObject qo){
        //按照发布的时间来排序，越迟发布的在上面
        qo.setOrderBy("t.releaseTime desc");
        //只查询通过审核的
        qo.setState(Travel.STATE_RELEASE);
        //查询到全部的数据
        return travelService.query(qo);
    }

    /**
     * 获取指定的游记
     * 参数：无
     * 资源：/travels/{id}
     * 动作：GET
     */
    @GetMapping("/{id}")
    public Object getTravelsById(@PathVariable Long id){
        return travelService.get(id);
    }
    /**
     获取当前周/月的推荐：
     资源：/travels/commends
     动作：GET
     参数：type
     */
    @GetMapping("/commends")
    public Object listCommendByType(@RequestParam Integer type){
        return travelCommendService.queryCommendByType(type);
    }

    /**
     * 获取到浏览次数
     * 资源: /travels/{id}.browses
     * 动作: GET
     * 参数:
     */
    @GetMapping("/{id}/browses")
    public Object getBrowseByTravelId(@PathVariable Long id){
        return travelService.getBrowse(id);
    }

    /**
     * 新增浏览次数
     * 参数:
     * 资源: /travels/{id}/browses
     * 动作: POST
     */
    @PostMapping("/{id}/browses")
    public Object saveBrowse(Travel travel){
        travelService.saveOrUpdateBrowse(travel);
        return new JasonResult();
    }

    /**
     * 修改浏览次数
     * 参数:
     * 资源: /travels/{id}/browses
     * 动作: PUT
     */
    @PutMapping("/{id}/browses")
    public Object updateBrowse(Travel travel){
        travelService.saveOrUpdateBrowse(travel);
        return new JasonResult();
    }

}
